﻿using Domain.Entities;
using JFnetcore.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utility;

namespace JFnetcore.Controllers
{
    public class BaseController : Controller
    {
        public SessionModel getUserSession()
        {
            string userJson = HttpContext.Session.GetString("CurrentUser");
            if (string.IsNullOrEmpty(userJson))
            {
                return null;
            }
            SessionModel userSession = JsonConvert.DeserializeObject<SessionModel>(userJson);
            return userSession;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            byte[] result;
            filterContext.HttpContext.Session.TryGetValue("CurrentUser", out result);
            if (result == null)
            {

                //filterContext.Result = new RedirectResult(ReturnJsonHelp.BJUIajaxJson(301, "Session超时！", "", "", "", false, "", ""));
                filterContext.Result = new RedirectResult("/Login/Index");
                return;

            }
            SessionModel userSession = getUserSession();
            if (userSession != null && userSession.User.Disable == 1)
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult("/Login/Index");
                return;
            }
        }

    }
}
